time: 2021.3
# -*- coding: utf-8 -*-
# @Time    : 2021/4/10 15:49
# @Author  : luogan
# @Email   : 468640092@qq.com
# @File    : 词频统计.py
# @Software: PyCharm.

def getText():
    txt = open("Walden.txt", "r",encoding='utf-8').read()  # 读取Hamlet文本文件，并返回给txt
    txt = txt.lower()                     # 将文件中的单词全部变为小写
    for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
        txt = txt.replace(ch, " ")        # 将文本中特殊字符替换为空格
    return txt
hamletTxt = getText()
words = hamletTxt.split()                  # 按照空格，将文本分隔
counts = {}
for word in words:
    counts[word] = counts.get(word, 0) + 1  # 先给字典赋值，如果字典中没有word这个键，则返回0
items = list(counts.items())                # 转换为列表
items.sort(key=lambda x: x[1], reverse=True)
print(items)
